/*
 *权限管理
 *接受当前用户loginUser和页面所需要的权限needAccess
 * return true/false
 * */
import ACCESS_ENUM from "@/access/accessEnum";

const checkAccess = (loginUser: any, needAccess: string) => {
  //获取当前用户全新啊
  const loginUserAccess = loginUser?.userRole ?? ACCESS_ENUM.NOT_LOGIN;
  //判断需要的权限
  //都可以访问直接返回true
  if (needAccess === ACCESS_ENUM.NOT_LOGIN) {
    return true;
  }
  //需要登录才能访问，如果没有登录，返回false
  if (needAccess === ACCESS_ENUM.USER) {
    if (loginUserAccess === ACCESS_ENUM.NOT_LOGIN) {
      return false;
    }
    return true;
  }
  //如果需要admin才能登录
  if (needAccess === ACCESS_ENUM.ADMIN) {
    if (loginUserAccess !== ACCESS_ENUM.ADMIN) return false;
    return true;
  }
};

export default checkAccess;
